﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using W.Purchase.API.Application.Command.ClientCommand;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;

namespace W.Purchase.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ClientController : ControllerBase
    {
        private readonly IMediator mediator;
        public ClientController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddAsync(ClientAddCommand client)
        {
            ApiResult<int> list= new ApiResult<int>();
            try
            {
                list.Code = await mediator.Send(client);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 添加联系人信息
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddContactAsync(ContactAddCommand contact)
        {
            ApiResult<int> list = new ApiResult<int>();
            try
            {
                list.Code = await mediator.Send(contact);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 反填联系人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<Contact>> FindClient(ContactFindCommand command)
        {
            ApiResult<Contact> list= new ApiResult<Contact>();
            try
            {
                list.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 修改联系人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateClient(ContactUpdateCommand command)
        {
            ApiResult<int> list = new ApiResult<int>();
            try
            {
                list.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 删除客户联系人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectClientConta(ClienContactDelCommand command)
        {
            ApiResult<int> list = new ApiResult<int>();
            try
            {
                list.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        [HttpGet]
        public async Task<ApiResult<ClientFanDTO>> FanClienConat([FromQuery] ClienContactFanCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
